package com.yjc.hire.server.controller;

import com.yjc.hire.exceptions.MyCustomException;
import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.mq.DelayConfig_Industry;
import com.yjc.hire.server.service.IndustryService;
import com.yjc.pojo.Industry;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/7/25
 * </p>
 */

@RestController
@RequestMapping("industry")
public class IndustryController {

    @Autowired
    private IndustryService industryService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping("createNode")
    public GraceJSONResult createNode(@RequestBody Industry industry){
        if (industryService.getIndustryExistByName(industry.getName()))
            GraceJSONResult.errorMsg("该行业已存在请出现命名");
        industryService.createIndustryNode(industry);
        industryService.resetRedisIndustry(industry);
        return GraceJSONResult.ok();
    }

    @GetMapping("getTopList")
    public GraceJSONResult getTopListNode(){
        List<Industry> industries = industryService.getIndusryTopNodeList();
        return GraceJSONResult.ok(industries);
    }

    @GetMapping("children/{industryId}")
    public GraceJSONResult getChildrenNodeList(@PathVariable("industryId") String  industryId){
        return GraceJSONResult.ok(industryService.getChildrenNodeList(industryId));
    }

    @PostMapping("updateLoad")
    public GraceJSONResult updateLoad(@RequestBody Industry industry){
        industryService.resetRedisIndustry(industry);
        industryService.updateNode(industry);
        return GraceJSONResult.ok();
    }
    @DeleteMapping("deleteNode/{industryId}")
    public GraceJSONResult deleteNode(@PathVariable String industryId) {
        try {
            industryService.deleteIndustryNode(industryId);
        }catch (MyCustomException e){
            return GraceJSONResult.errorMsg("请保证该节点下面无任何子节点在进行删除");
        }
        return GraceJSONResult.ok();
    }

    @GetMapping("app/initTopList")
    public GraceJSONResult initTopList(){
        return GraceJSONResult.ok(industryService.getIndusryTopNodeList());
    }
    @GetMapping("app/getThirdListByTop/{topIndustryId}")
    public GraceJSONResult getThirdListByTop(@PathVariable String topIndustryId){
        List<Industry> thirdList = industryService.getThirdList(topIndustryId);
        return GraceJSONResult.ok(thirdList);
    }

    @PostMapping("refreshIndustry")
    public GraceJSONResult refreshIndustry() {
        int delayTimes = 10 * 10000;
        MessagePostProcessor processor = DelayConfig_Industry.setDelayTimes(delayTimes);
        rabbitTemplate.convertAndSend(DelayConfig_Industry.EXCHANGE_DELAY_REFRESH,
                DelayConfig_Industry.DELAY_REFRESH_INDUSTRY,
                "",
                processor);
        return GraceJSONResult.ok();
    }
}
